package creational.singleton;

/**
 * 双重检查加锁，使用了 volatile 关键字，保证了单例的构造写入后，其他线程能够从主存中获取
 * 而不是从线程缓冲中获取。同时这样同步代码块只会执行一次
 * volatile 关键字的详解参考：他只适合使用在 JDK5 以上版本
 * http://www.cnblogs.com/java-my-life/archive/2012/03/31/2425631.html
 */
public class VolatileSingleton {

    private static volatile VolatileSingleton singleton;

    private VolatileSingleton() {

    }

    public static VolatileSingleton getInstance() {
        if (singleton == null) {
            synchronized (VolatileSingleton.class) {
                if (singleton == null) {
                    singleton = new VolatileSingleton();
                }
            }
        }

        return singleton;
    }

}
